package edu.scau.mis.web.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.scau.mis.cloth.entity.*;
import edu.scau.mis.cloth.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;

@RestController
@RequestMapping("GsReturn")
public class GsReturnController {
    @Autowired
    private GsReturnService gsReturnService;
    @Autowired
    private GsOrderService gsOrderService;
    @Autowired
    private GsOrderItemService gsOrderItemService;
    @Autowired
    private GsAttrsService gsAttrsService;
    @Autowired
    private GsPaymentService gsPaymentService;
    @Autowired
    private GsStorageService gsStorageService;


//    发起订单退货申请
    @GetMapping("{orderId}/{returnCause}")
    public boolean insert(@PathVariable("orderId") String orderId,@PathVariable("returnCause") String returnCause){
        if(gsReturnService.queryByOrderId(orderId)==null){
            //        根据orderId获得订单金额
            int money=gsOrderService.queryTotalMoney(orderId);
//        传入订单编号和退款金额 退款原因
            gsReturnService.insert(orderId,money,returnCause);
            return TRUE;
        }
        else{
            return FALSE;
        }
        }


//    显示全部退货申请

    @GetMapping("/page")
    public PageInfo page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<GsReturn> list = this.gsReturnService.queryAll();
        PageInfo<List> pageInfo = new PageInfo(list);
        return pageInfo;
    }

//    根据订单编号查询退货申请
    @GetMapping("{orderId}")
    public GsReturn queryByOrderId(@PathVariable("orderId") String orderId){
        return gsReturnService.queryByOrderId(orderId);
    }

//    根据退货状态显示退货申请

    @GetMapping("state/{returnState}")
    public PageInfo queryByReturnState(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize,@PathVariable("returnState") String returnState){
         PageHelper.startPage(pageNum,pageSize);
         List list=gsReturnService.queryByReturnState(returnState);
         PageInfo<List> pageInfo = new PageInfo(list);
         return pageInfo;
     }

//     将订单中的商品放回
    public void returnItem(String orderId){
//        将库存加回来 得到对应的订单项进行循环 取出cl_id cl_size和quantity进行库存修改
        List<Map<String,Object>> list=gsOrderItemService.queryByOrderId(orderId);
        list.forEach(i->{
            int clId=(Integer)i.get("clId");
            int quantity= (int) Double.parseDouble(i.get("quantity").toString());
            String clSize=i.get("clSize").toString();
            int inventory=gsAttrsService.getInventory(clId,clSize);
            gsAttrsService.updateInventory(clId,clSize,inventory+quantity);
        });
    }
//     对订单进行处理确认退货修改订单的处理时间和状态 同时gs_order gs_order_item中删除相关订单 增加入库记录 对应商品的库存进行修改
    @GetMapping("agree/{orderId}")
    public void agreeReturn(@PathVariable("orderId")String orderId){
        //        更改订单状态为已退货
        gsReturnService.agreeReturn(orderId);
//         放回商品
        this.returnItem(orderId);
//        对订单添加入库记录
        List<Map<String,Object>> list=gsOrderItemService.queryByOrderId(orderId);
        list.forEach(i->{
            int clId=(Integer)i.get("clId");
            int quantity= (int) Double.parseDouble(i.get("quantity").toString());
            String clSize=i.get("clSize").toString();
            gsStorageService.returnInsert(clId,clSize,quantity);
        });
//         gs_order删除对应order
        gsOrderService.deleteById(orderId);
//         gs_order_item删除对应订单项
        gsOrderItemService.deleteById(orderId);
//        注意删除相应的支付记录
        gsPaymentService.deleteById(orderId);
    }

//    取消订单
    @GetMapping("cancel/{orderId}")
    public void cancelOrder(@PathVariable("orderId")String orderId){
//        放回商品
         this.returnItem(orderId);
         gsOrderService.deleteById(orderId);
//         gs_order_item删除对应订单项 用来统计已经销售信息
        gsOrderItemService.deleteById(orderId);

    }


//    拒绝订单退货 更改处理时间和订单状态
    @GetMapping("refuse/{orderId}")
    public int refuseReturn(@PathVariable("orderId") String orderId){
        return gsReturnService.refuseReturn(orderId);
    }
}


